{{ $current_page := . }}

{{/* Get site logo. */}}
{{ $show_logo := site.Params.main_menu.show_logo | default true }}
{{ $has_logo := fileExists "assets/images/logo.png" | or (fileExists "assets/images/logo.svg") }}
{{ $logo := "" }}
{{ if $has_logo }}
  {{ $logo = (partial "functions/get_logo" (dict "constraint" "max_height" "size" 70)) }}
{{ end }}
{{ $logo_visibility := "hidden"}}
{{ if $show_logo }}
  {{ $logo_visibility = "visible"}}
{{ end }}

<nav class="navbar navbar-expand-lg navbar-light compensate-for-scrollbar" id="navbar-main">
  <div class="container">

    <div class="d-none d-lg-inline-flex">
      <a style="visibility:{{ $logo_visibility }};" class="navbar-brand" href="{{ "/" | relLangURL }}">
        {{- if $has_logo -}}
          <img src="{{ $logo.RelPermalink }}" alt="{{ site.Title }}">
        {{- else -}}
          {{- site.Title -}}
        {{- end -}}
      </a>
    </div>


    {{ if site.Menus.main }}
    <button type="button" class="navbar-toggler" data-bs-toggle="collapse" data-bs-target="#navbar-content-mobile" aria-controls="navbar" aria-expanded="false" aria-label="{{ i18n "toggle_navigation" }}">
      <span><i class="fas fa-bars"></i></span>
    </button>
    {{ end }}

    {{if $show_logo}}
    <div class="navbar-brand-mobile-wrapper d-inline-flex d-lg-none">
      <a class="navbar-brand" href="{{ "/" | relLangURL }}">
      {{- if $has_logo -}}
      <img src="{{ $logo.RelPermalink }}" alt="{{ site.Title }}">
      {{- else -}}
      {{- site.Title -}}
      {{- end -}}
      </a>
    </div>
    {{end}}

    <!-- Collect the nav links, forms, and other content for toggling -->
    {{ $align_menu := site.Params.main_menu.align | default "l" }}
    <div id="navbar-content" class="navbar-collapse main-menu-item collapse {{ if eq $align_menu "c" }}justify-content-center{{ else if eq $align_menu "r" }}justify-content-end{{else}}justify-content-start{{ end }}">

    <!-- Left Nav Bar -->
    <ul class="navbar-nav d-md-inline-flex">
      {{ range site.Menus.main }}

      {{ if .HasChildren }}
      <li class="nav-item dropdown">
        <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true">
          {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
          <span class="caret"></span>
        </a>
        <div class="dropdown-menu">
          {{ range .Children }}
          <a class="dropdown-item" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}>
            {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
          </a>
          {{ end }}
        </div>
      </li>

      {{ else }}

      {{/* Set target for link. */}}
      {{ $.Scratch.Set "target" "" }}
      {{ if gt (len .URL) 4 }}
      {{ if eq "http" (slicestr .URL 0 4) }}
      {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
      {{ end }}
      {{ end }}

      {{/* Get active page. */}}
      {{ $is_link_in_current_path := in $current_page.RelPermalink .URL }}
      {{ $is_widget_page := or $current_page.IsHome (eq $current_page.Type "widget_page") }}
      {{ $hash := findRE "#(.+)" .URL }}
      {{ $is_same_page := $is_link_in_current_path }}
      {{ if gt (len $hash) 0 }}
      {{ $hash = index $hash 0 }}
      {{ $hash_removed := replace .URL $hash "" }}
      {{ if eq (len $hash_removed) 0 }}
      {{ $hash_removed = "/" }}{{/* Add robustness for `/#SECTION` or `#SECTION` in `menus.toml`. */}}
      {{ end }}
      {{ $is_same_page = eq (path.Dir $current_page.RelPermalink) (path.Dir ($hash_removed|relLangURL)) }}
      {{ end }}

      <li class="nav-item">
        <a class="nav-link {{if $is_link_in_current_path }} active{{end}}" href="{{.URL}}"{{ if and $is_widget_page $is_same_page }} data-target="{{$hash}}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
        {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
        </a>
      </li>

      {{ end }}
      {{ end }}

      {{ if site.Menus.main_right | and (eq $align_menu "l") }}
    </ul>
    <ul class="navbar-nav ml-md-auto">
      {{ end }}

      {{ range site.Menus.main_right }}

      {{/* Set target for link. */}}
      {{ $.Scratch.Set "target" "" }}
      {{ if gt (len .URL) 4 }}
      {{ if eq "http" (slicestr .URL 0 4) }}
      {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
      {{ end }}
      {{ end }}

      <li class="nav-item">
        <a class="nav-link" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
        {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
        </a>
      </li>

      {{ end }}
    </ul>
  </div><!-- /.navbar-collapse -->

    <ul class="nav-icons navbar-nav flex-row ml-auto d-flex pl-md-2">
      {{ if site.Params.search.engine }}
      <li class="nav-item">
        <a class="nav-link js-search" href="#" aria-label="{{ i18n "search" }}"><i class="fas fa-search" aria-hidden="true"></i></a>
      </li>
      {{ end }}

      {{ if site.Params.day_night }}
      <li class="nav-item dropdown theme-dropdown">
        <a href="#" class="nav-link js-theme-selector" data-toggle="dropdown" aria-haspopup="true">
          <i class="fas fa-palette" aria-hidden="true"></i>
        </a>
        <div class="dropdown-menu">
          <a href="#" class="dropdown-item js-set-theme-light">
            <span>{{ i18n "theme_light" | default "Light" }}</span>
          </a>
          <a href="#" class="dropdown-item js-set-theme-dark">
            <span>{{ i18n "theme_dark" | default "Dark" }}</span>
          </a>
          <a href="#" class="dropdown-item js-set-theme-auto">
            <span>{{ i18n "theme_auto" | default "Automatic" }}</span>
          </a>
        </div>
      </li>
      {{ end }}

      {{ if site.Params.github }}
      <li class="nav-item ">
        <a href="{{ site.Params.github }}" target="blank">
          <i class="fab fa-github" aria-hidden="true"></i>
        </a>
      </li>
      {{ end }}

      {{ if .IsTranslated }}
      <li class="nav-item dropdown i18n-dropdown">
        <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true">
          <i class="fas fa-globe mr-1" aria-hidden="true"></i><span class="d-none d-lg-inline">{{ index site.Data.i18n.languages .Lang }}</span>
        </a>
        <div class="dropdown-menu">
          <div class="dropdown-item dropdown-item-active">
            <span>{{ index site.Data.i18n.languages .Lang }}</span>
          </div>
          {{ range .Translations }}
          <a class="dropdown-item" href="{{ .Permalink }}"{{ if $.IsHome }} data-target="{{ .RelPermalink }}"{{ end }}>
            <span>{{ index site.Data.i18n.languages .Lang }}</span>
          </a>
          {{ end }}
        </div>
      </li>
      {{ end }}

    </ul>

    <!-- Collect the nav links, forms, and other content for toggling -->
    {{ $align_menu := site.Params.main_menu.align | default "l" }}
    <div id="navbar-content-mobile" class="navbar-collapse main-menu-item collapse {{ if eq $align_menu "c" }}justify-content-center{{ else if eq $align_menu "r" }}justify-content-end{{else}}justify-content-start{{ end }} d-lg-none">

    <!-- Left Nav Bar -->
      <ul class="navbar-nav d-inline-flex">
        {{ range site.Menus.main }}

        {{ if .HasChildren }}
        <li class="nav-item dropdown">
          <a href="#" class="nav-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true">
            {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
            <span class="caret"></span>
          </a>
          <div class="dropdown-menu">
            {{ range .Children }}
              <a class="dropdown-item" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}>
                {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
              </a>
            {{ end }}
          </div>
        </li>

        {{ else }}

        {{/* Set target for link. */}}
        {{ $.Scratch.Set "target" "" }}
        {{ if gt (len .URL) 4 }}
          {{ if eq "http" (slicestr .URL 0 4) }}
            {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
          {{ end }}
        {{ end }}

        {{/* Get active page. */}}
        {{ $is_link_in_current_path := in $current_page.RelPermalink .URL }}
        {{ $is_widget_page := or $current_page.IsHome (eq $current_page.Type "widget_page") }}
        {{ $hash := findRE "#(.+)" .URL }}
        {{ $is_same_page := $is_link_in_current_path }}
        {{ if gt (len $hash) 0 }}
          {{ $hash = index $hash 0 }}
          {{ $hash_removed := replace .URL $hash "" }}
          {{ if eq (len $hash_removed) 0 }}
            {{ $hash_removed = "/" }}{{/* Add robustness for `/#SECTION` or `#SECTION` in `menus.toml`. */}}
          {{ end }}
          {{ $is_same_page = eq (path.Dir $current_page.RelPermalink) (path.Dir ($hash_removed|relLangURL)) }}
        {{ end }}

        <li class="nav-item">
          <a class="nav-link {{if $is_link_in_current_path }} active{{end}}" href="{{.URL}}"{{ if and $is_widget_page $is_same_page }} data-target="{{$hash}}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
            {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
          </a>
        </li>

        {{ end }}
        {{ end }}

      {{ if site.Menus.main_right | and (eq $align_menu "l") }}
      </ul>
      <ul class="navbar-nav ml-md-auto">
      {{ end }}

        {{ range site.Menus.main_right }}

        {{/* Set target for link. */}}
        {{ $.Scratch.Set "target" "" }}
        {{ if gt (len .URL) 4 }}
        {{ if eq "http" (slicestr .URL 0 4) }}
        {{ $.Scratch.Set "target" " target=\"_blank\" rel=\"noopener\"" }}
        {{ end }}
        {{ end }}

        <li class="nav-item">
          <a class="nav-link" href="{{ .URL | relLangURL }}"{{ if $.IsHome }} data-target="{{ .URL }}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
            {{- .Pre -}}<span>{{ .Name | safeHTML }}</span>{{- .Post -}}
          </a>
        </li>

        {{ end }}
      </ul>
    </div><!-- /.navbar-collapse -->

  </div><!-- /.container -->
</nav>
